#ifndef FILA_H
#define FILA_H

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

#include "No.h"                                                                                                                                  
template <class T>class Fila{
 private:
     	NO<T> *inicio;
     	NO<T> *fim;
	int count;

 public: 
    	Fila(){
		this->inicio=NULL;
    		this->fim=NULL;
    		this->count = 0;
	}
	
	int getSize(){
		return this->count;
	}

    	int vazia(){
		if (this->inicio==NULL)
      			return(1);
      		else
      			return(0);
	}

    	int insere(T data){
		this->count++;
        	NO<T> *aux = new NO<T>;
		aux->data = data;
                                                                                
        	if (aux == NULL)
                	return(0);
                                                                                
        	if (this->inicio==NULL)
                	this->inicio=aux;
                                                                                
        	if (this->fim!=NULL){
                	this->fim->prox=aux;
                	if(this->count == 2)
                        	this->inicio->prox = aux;
        	}
                                                                                
        	this->fim=aux;
        	return(1);
	}

	int insereOrdenado(T data){
		this->count++;
                No *aux = new No(data);

                if (aux == NULL)
                        return(0);

                if (this->inicio==NULL)
                        this->inicio=aux;
                else{
                        NoSolucao *ant, *suc;
                        ant = this->inicio; suc = this->inicio->prox;

                        while(ant->data < data){
                                if(suc == NULL){
                                        ant->prox = aux;
                                        return(1);
                                }else{
                                        if(data < suc->data){
                                                ant->prox = aux; aux->prox = suc;
                                                return(1);
                                        }else{
                                  		ant = suc; suc = suc->prox;
                                        }
                                }
                        }
                        aux->prox = ant;
                        this->inicio = aux;
                }
                return(1);
	}

    	T remove(){
		this->count--;
        	NO<T> *aux;
                                                                                
        	if (this->vazia())
                	return 0;
                                                                                
        	T data = this->inicio->data;
        	aux=this->inicio;
       	
		this->inicio=this->inicio->prox;
                                                                                
        	if(this->inicio == NULL)
                	this->fim = NULL;
        	//delete(aux);
                                                                                
        	return data;
	}
	T get(int ind){
		if(ind >= this->count)
			return 0;
		NO<T> *aux = this->inicio;
		for(int i = 0; i < ind; i++)
			aux = aux->prox;
		return aux->data;
	}
};

#endif
